﻿using System;
using System.Linq.Expressions;

namespace SPFluid.Core.Validation
{
    internal class Argument<T> : IArgument<T>
    {
        public Argument(T argument)
            : this(argument, null)
        {
        }

        public Argument(T argument, string argumentName)
        {
            this.Value = argument;
            this.Name = argumentName;
        }

        public Argument(Expression<Func<T>> argument)
        {
            this.Value = GetArgumentValue(argument);
            this.Name = GetArgumentName(argument);
        }

        public T Value { get; protected set; }

        public string Name { get; protected set; }

        protected static string GetArgumentName(Expression<Func<T>> argument)
        {
            return ((MemberExpression)argument.Body).Member.Name;
        }

        protected static T GetArgumentValue(Expression<Func<T>> argument)
        {
            return argument.Compile()();
        }
    }
}